package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.minio.MinIOService;
import com.heima.common.util.UserThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author lj
 * @since 2022-06-28
 */
@Slf4j
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {

    @Autowired
    private MinIOService minIOService;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @Override
    public ResponseResult upload(MultipartFile file) {
        log.info("[自媒体图文素材信息表实现类======>图片上传，接收参数：{}]",file.toString());
        // 获取当前用户
        User user = UserThreadLocalUtil.get();
        if (user==null) {
            ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN); //未登录
        }
        try {
            // 调用minIOService保存图片
            String fileUrl = minIOService.upload(file);
            // 设置自媒体图文素材信息表参数
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUrl(fileUrl);
            wmMaterial.setUserId(user.getUserId());
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setType(0); // 素材类型 0 图片 1 视频
            wmMaterial.setIsCollection(false);
            // 保存
            this.save(wmMaterial);
            return ResponseResult.okResult(wmMaterial);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    /**
     * 素材列表展示
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listWmMaterial(WmMaterialDto dto) {
        log.info("[自媒体图文素材信息表实现类======>素材列表展示，接收参数：{}]",dto.toString());
        // 获取当前用户
        User user = UserThreadLocalUtil.get();
        if (user==null) {
            ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN); //未登录
        }
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 当前页码，页码大小，是否收藏
        Long page = dto.getPage();
        Integer size = dto.getSize();
        Integer isCollection = dto.getIsCollection();

        // 分页构造器
        Page<WmMaterial> iPage = new Page<>(page,size);

        // 条件构造器
        LambdaQueryWrapper<WmMaterial> queryWrapper= new LambdaQueryWrapper<>();
        if (isCollection!=null) {
            queryWrapper.eq(WmMaterial::getIsCollection,isCollection);
        }
        // 隐藏条件
        queryWrapper.eq(WmMaterial::getUserId,user.getUserId());
        // 结果排序
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        // 分页查询
        Page<WmMaterial> pageInfo = this.page(iPage, queryWrapper);
        // 返回结果集
        return new PageResponseResult(page,size,pageInfo.getTotal(),pageInfo.getRecords());
    }
}
